﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GameController : MonoBehaviour
{
    //陨石初次生成的速度
    public float spawnFirstBornWaitTime = 3f;
    //陨石生成的间隔速度
    public float spawnRepeatWaitTime = 1f;
    //每波陨石生成的数量
    public int spawnRepeatNumber = 10;
    //生成陨石的位置
    public Vector3 spawnValue = new Vector3(7f,0,20f);
    //创建陨石实体数组
    public GameObject[] Hazards;
    //创建3D Text组件
    public GameObject D3Text;
    //初始化分数
    private int score = 0;
    //声明Restart Text类型
    public GameObject restartText;
    //声明GameOver Text类型
    public GameObject gameOverText;
    //声明ESC_Text类型
    public GameObject escText;
    //声明游戏是否结束
    public bool GameOver = false;
    //声明闪烁
    public bool Blink = true;
    //加入GUI
    // Start is called before the first frame update
    void Start()
    {
        Debug.Log("Game Start......");
        InvokeRepeating("Spawn", spawnFirstBornWaitTime, spawnRepeatWaitTime);
        //启用协助线程，Ienumerator类型的函数必须用协助线程才能启动
        //StartCoroutine(SpawnRepeatBorn());
        Debug.Log("Wait...-1");
    }
    void Update()
    {
        if (GameOver) {
            //启动协助线程，避免卡死
            BlinkObject(restartText);
            //BlinkObject(escText);
            escText.SetActive(true);
            gameOverText.SetActive(true);
        }
        //按下R键，游戏再启动
        if (GameOver && Input.GetKeyDown(KeyCode.R)) {
            Application.LoadLevel(Application.loadedLevel);
        }
        //按下ESC键，退出游戏
        if (GameOver && Input.GetKeyDown(KeyCode.Escape)) {
            Application.Quit(); 
        }
    }

    //生成一波陨石，陨石的数量自定义
    IEnumerator SpawnRepeatBorn() {
        Debug.Log("Wait...0");
        //陨石初次生成等待时间
        new WaitForSeconds(spawnFirstBornWaitTime);
        for (int i=0;i<spawnRepeatNumber;i++) {
            //如果游戏结束，停止生成陨石
            //if (GameOver) { break ; }
            Debug.Log("Wait...1");
            Spawn();
            //陨石生成间隔时间
            Debug.Log("Wait...2");
            yield return new WaitForSeconds(spawnRepeatWaitTime);
        }
    }
    //生成陨石的方法
    void Spawn() {
        //计算实体数组的长度
        int HLength = Hazards.Length;
        //实体数组随机选中一个
        GameObject o = Hazards[(int)Random.Range(0,HLength)];
        //设定实体的生成坐标
        Vector3 vector = new Vector3(Random.Range(-spawnValue.x,spawnValue.x),spawnValue.y,spawnValue.z);
        //四元数，用来计算旋转。
        Quaternion q = Quaternion.identity;
        //生成实体
        if (!GameOver) { 
            Instantiate(o,vector,q);
        }
    }
    //统计分数
    public void AddScore(int number) {
        //添加分数
        score += number;
        //将分数发送到3D Text上面
        D3Text.gameObject.GetComponent<TextMesh>().text = "SCORE: " + score;
    }

    //激活并闪烁text实体
    void BlinkObject(GameObject other) {
        if (Blink)
        {
            other.SetActive(true);
            Blink = false;
            //yield return new WaitForSeconds(2f);
        }
        else {
            other.SetActive(false);
            Blink = true;
            //yield return new WaitForSeconds(2f);
        }
        
    }
    
}
